class MyPage extends BaseList {
    constructor(orderby, flag, $pagesize, $pagination, listUrl, onLineEditUrl, checkBoxName) {
        super(orderby, flag, $pagesize, $pagination, listUrl, onLineEditUrl, checkBoxName);
    }
    GetFilter(pageIndex) {
        let item = super.GetFilter(pageIndex);
        // 设置检索
        item['MaterialNameFilterValue'] = formatNullInputValue($.trim($("#MaterialNameFilterValue").val()));
        item['MaterialNoFilterValue'] = formatNullInputValue($.trim($("#MaterialNoFilterValue").val()));
        item['Begin'] = formatNullInputValue($.trim($("#Begin").val()));
        item['End'] = formatNullInputValue($.trim($("#End").val()));
        item['CheckedStatus'] = parseInt($("#CheckedStatus").val()) === -1 ? null : parseInt($("#CheckedStatus").val());
        item['CurrentHeaderId'] = parseInt(parent.$("#HeaderId").val());
        return JSON.stringify(item);
    }
    FetchAll() {
        $("#Begin").val("");
        $("#End").val("");
        $("#MaterialNoFilterValue").val("");
        $("#MaterialNameFilterValue").val("");
        $("#CheckedStatus").val("-1");

        this.$pagination.attr("_msh_page", 1);
        this.FetchForPage(1);
    }
    EmptyTableContent() {
        $("#tabData01 tbody").empty();
        this.$pagination.attr("_msh_page", 1);
    }
    SetTableContent(result) {
        $("#tabData01 tbody").empty();
        let content_01 = '';

        this.rows = result.length;  // 设置表格中checkbox的数量为表格的行数
        this.numCheckbox = result.length;  // 设置表格中checkbox的数量为表格的行数
        for (let i = 0; i < result.length; i++) {
            let _headerId = result[i].headerId;
            content_01 = '<tr _mash_id="' + _headerId + '">';
            if (parseInt(this.flag) === 0) { // 单选
                content_01 += '<td style="padding-left:8px; padding-top:0px; vertical-align:baseline;"><label class="btn" for="R-' + _headerId + '"><input id="R-' + _headerId + '" name="_check_row" type="radio" onclick="_Instance.OnChoice($(this));" value="' + _headerId + '"/><i></i><span></span></label></td>';
            } else { // 多选
                content_01 += '<td style="padding-left:9px;"><label class="btn" for="R-' + _headerId + '"><input id="R-' + _headerId + '" name="_check_row" type="checkbox" onclick="_Instance.DoCheckRow($(this));" value="' + _headerId + '"/><i></i><span></span></label></td>';
            }
            content_01 += '<td>' + formatNullData(result[i].billNo) + '</td>';
            content_01 += '<td>' + formatNullData(result[i].feedingTypeName) + '</td>';
            // 有审核人则是审核中的状态，否则未审核状态
            if (result[i].checkedBy === null) {
                content_01 += '<td style="text-align:center;"><span class="c2"></span></td>';
            } else {
                if (parseInt(result[i].checkedStatus) === 1) {
                    // 最终审核
                    content_01 += '<td style="text-align:center;"><span class="c1"><span></td>';
                } else {
                    content_01 += '<td style="text-align:center;"><span class="c3"></span></td>';
                }
            }
            let _isFinished = '';
            if (result[i].poOverFlag === true) {
                _isFinished = "<span class='c1'><span>";
            } else {
                if (result[i].quotedFlag === false) {
                    _isFinished = "<span class='c2'><span>";
                } else {
                    _isFinished = "<span class='c3'><span>";   // 表示完成一部分，还在采购中
                }
            }
            content_01 += '<td style="text-align:center;">' + _isFinished + '</td>';
            content_01 += '<td>' + formatNullData(result[i].scheduledDate) + '</td>';
            content_01 += '<td>' + formatNullData(result[i].scheduledByName) + '</td>';
            content_01 += '<td>' + formatNullData(result[i].updatedByName) + '</td>';
            content_01 += '<td>' + formatNullData(result[i].updatedDate) + '</td>';
            //content_01 += '<td>' + formatNullData(result[i].checkedByName) + '</td>';
            content_01 += '<td style="border-right: none;">' + formatNullData(result[i].description) + '</td>';
            content_01 += '</tr>';

            $("#tabData01 tbody").append(content_01);
            content_01 = '';
        }
        setOneTableRowColor("tabData01");
        // 针对多选的恢复选定结果
        if (parseInt(this.flag) === 1) {
            this.ResetSelectRowStatus();
        }
    }
    // 选择后执行的动作
    SetChoiceData($self) {
        // 设置选择结果
        let $tr1 = $self.parent().parent().parent();

        //设置选择结果
        return new Array($self.val(),
                $.trim($tr1.find('td').eq(1).text()));  // 系列号
    }
    // 设置行选择后的状态
    SetRowStatus($self) {
        super.AdjustRowStatus($self, ($row, index) => {
            let key = $self.val();
            let item = {};
            item["mrpFeedingHeaderId"] = key;
            this.selectedItemList[key] = item;
        }, ($row, index) => {
            let key = $self.val();
            delete this.selectedItemList[key];
        });
    }
    // 每次翻页都恢复选定结果
    ResetSelectRowStatus() {
        let _this = this;
        $("#tabData01 tbody").find("tr").each(function (index) {
            let key = $(this).attr("_mash_id");
            if (_this.selectedItemList[key] !== undefined) {
                $(this).children("td").eq(0).children("label").eq(0).children("input[type='checkbox'][name='_check_row']").eq(0).prop('checked', true);
                $(this).addClass("row__selected");
            }
        });
        // 设置选择全部的状态
        let n = $("input[type='checkbox'][name='_check_row']:checked").length;
        if (n < $("#tabData01 tbody").find("tr").length || n === 0) {
            $("#CheckAll").prop("checked", false);
        } else {
            $("#CheckAll").prop("checked", true);
        }
    }
    SortTable(orderBy) {
        this.orderby = orderBy;
        // 得到排序数据
        this.FetchForPage(1);
    }
}

let _Instance = null;

//-----------------------------------------------------------------------------
// 页面初始化
//-----------------------------------------------------------------------------
function onPageReady() {
    let flag = jQuery.url.param("flag") === undefined ? 0 : jQuery.url.param("flag");
    _Instance = new MyPage(null, flag, null, $("abbr.page_indicator_block,#wrapper__navbar"),
            "/tech/mrp/feeding/fetchForPage.html",
            "",
            "_check_row");

    // 设置分页控制 （操作区域）
    $("#wrapper__navbar").swPagination({
        link_string: '/?page={page_number}',
        max_page: null,
        paged: function (page) {
            $("abbr.page_indicator_block,#wrapper__navbar").attr("_msh_page", page);
            _Instance.FetchForPage(page);
        }
    });
    window.laydate.render({
        elem: '#Begin',
        theme: '#393D49',
        trigger: 'click'
    });
    window.laydate.render({
        elem: '#End',
        theme: '#393D49',
        trigger: 'click'
    });
    _Instance.OnPageLoad();
}

// 画面大小或者位置移动
function onPageResize() {
}